﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ScFramework.Core;
using System;
using CollisionHandler = System.Action<CollisionArgs, ISharedCollisionManager>;
public partial class CollisionManager:ISharedCollisionManager
{

    public BasicInfo GetInfo(BelongingSystem sys, uint id)
    {
        if (sys == BelongingSystem.Default)
        {
            var tp = new SComValuesVEx(EntityDataBase.GetEntityData(id.left()).GetComponentData<SComValues>());
            return new BasicInfo(tp.Blood, tp.Defence, tp.MagicDefence, tp.Sharpness, tp.Mass, EntityDataBase.GetEntityData(id.left()).GetComponentData<STransform>().GetValue<Vector3>("LocalPosition"));

        }
        else
        {
            return subPhysicsManager.GetInfo(sys, id);
        }
    }

    public void Destory(BelongingSystem sys, uint id)
    {
        if (sys == BelongingSystem.Default)
        {
            Dispatch(EntityDataEvent.Delete, id.left(),false);
        }
        else
        {
            subPhysicsManager.Delete(sys, id);
        }
    }

    public bool Damage(BelongingSystem sys, uint id, int dam)
    {
        if (dam == 0) return false;
        else
        {
            var info = GetInfo(sys, id);
            var blood = info.Blood;
            if (dam >= blood)
            {
                Destory(sys, id);
                return true;
            }
            else
            {
                if (BelongingSystem.Default == sys)
                {
                     EntityDataBase.GetEntityData(id.left()).GetComponentData<SComValues>().SetValue<int>("Blood",blood - dam);

                }
                else
                {
                    info.Blood = blood;
                    subPhysicsManager.SetInfo(sys, id, info);
                }
                return false;
           

            }
        }
    }
    public List<Tuple<BelongingSystem, uint>> GetOverlapBox(Vector2 pos, Vector2 siz, float angle,int layer)
    {
        var list = Physics2D.OverlapBoxAll(pos, siz, angle,layer);

        List<Tuple<BelongingSystem, uint>> ans = new List<Tuple<BelongingSystem, uint>>();
        foreach (var x in list)
        {
            if (x.gameObject.GetComponent<SCollider>())
            {
                var c = x.gameObject.GetComponent<SCollider>();

                ans.UniqueAdd(new Tuple<BelongingSystem, uint>(BelongingSystem.Default, c.SData.CompIdentity));
            }

        }
        ans.AddRange(subPhysicsManager.GetOverlapBox(pos, siz, angle));
        return ans;
    }

    public  uint Create(BelongingSystem sys, ElementState state, Vector2 pos)
    {
        if (sys == BelongingSystem.Default)
        {
            throw new System.Exception();
        }
        else
        {
            return subPhysicsManager.Create(sys, state, pos);
        }
    }

    public void MagicAttack(BelongingSystem sys, uint id, int power)
    {
        Damage(sys, id, Math.Max(power - GetInfo(sys, id).MagicDefence,1));
    }
}


public class BasicInfo
{
    public int Blood;
    public int Defence;
    public int MagicDefence;
    public int Sharpness;
    public int Mass;
    public Vector3 Position;

    public BasicInfo(int b,int d,int m,int s,int ma,Vector3 p)
    {
        Blood = b;
        Defence = d;
        MagicDefence = m;
        Sharpness = s;
        Mass = ma;
        Position = p;
    }
}

public interface ISharedCollisionManager : IModuleInnerInterface
{
    List<Tuple<BelongingSystem, uint>> GetOverlapBox(Vector2 pos, Vector2 siz, float angle, int layer);
    CollisionHandler GetHandler(ElementState stressing, ElementState stressed);
    BasicInfo GetInfo(BelongingSystem sys, uint id);
    void Destory(BelongingSystem sys, uint id);
    uint Create(BelongingSystem sys, ElementState state, Vector2 pos);

    void MagicAttack(BelongingSystem sys, uint id, int power);

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sys">233</param>
    /// <param name="id"></param>
    /// <param name="dam"></param>
    /// <returns>true means destoryed</returns>
    bool Damage(BelongingSystem sys, uint id, int dam);
}